﻿using AuroraPlatform;
using AuroraPlatform.Tools;
using AuroraPlatform.ViewModel;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Browser;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

namespace AuroraPlatform.View.Level4
{
    public partial class LightingVersionView : UserControl, IPageProvider, IExportData
    {
        private ObservableCollection<LightingPrimaryModel> _items = new ObservableCollection<LightingPrimaryModel>();
        private NaviTo4Param _parameter;

        public LightingVersionView()
        {
            InitializeComponent();

            for (int i = 0; i < 21; i++)
            {
                Grid.RowDefinitions.Add(new RowDefinition() { Height = new GridLength(1, GridUnitType.Star) });
            }

            Pictures.ItemsSource = _items;
            Pictures.SelectionChanged += Pictures_SelectionChanged;

            Loading();
        }

        public async void ExportToExcel()
        {
            if (_parameter == null)
                return;
            
            busy.IsBusy = true;
            busy.BusyContent = "正在导出数据，请稍等......";

            WebServiceProxy proxy = new WebServiceProxy();
            string url = await proxy.SearchPrimaryProdInfoToExcel(_parameter.ID);
            busy.IsBusy = false;

            if(!string.IsNullOrEmpty(url))
                HtmlPage.Window.Navigate(new Uri(url));
            else
                MessageHelper.ShowError("导出数据出错。");
        }

        public async void ExportToPdf()
        {
            if (_parameter == null)
                return;

            busy.IsBusy = true;
            busy.BusyContent = "正在导出数据，请稍等......";

            WebServiceProxy proxy = new WebServiceProxy();
            string url = await proxy.SearchPrimaryProdInfoToPDF(_parameter.ID);
            busy.IsBusy = false;

            if (!string.IsNullOrEmpty(url))
                HtmlPage.Window.Navigate(new Uri(url));
            else
                MessageHelper.ShowError("导出数据出错。");
        }

        private void Pictures_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            LightingPrimaryModel lighting = (LightingPrimaryModel)Pictures.SelectedItem;
            BusinessContext.Singleton.ParamIterator.GoNew(
                new NaviTo4Param()
                { 
                    ID = lighting.ID,
                    CurrentUrl = MainPage.Level4Uri
                    //SourceUrl = MainPage.Level4Uri
                });
            Loading();
        }

        private LightingPrimaryModel ConvertForm(PrimaryProd lighting)
        {
            LightingPrimaryModel vm = new LightingPrimaryModel();
            vm.ID = lighting.Id;
            vm.ProdClass = lighting.ProdClass;
            vm.ProdPlace = lighting.ProdPlace;
            vm.ProdSeries = lighting.ProdSeries;
            vm.ProdVersion = lighting.ProdVersion;
            vm.ProdLabel = lighting.ProdLabel;
            vm.PhyColor = lighting.PhyColor;
            vm.PhySize = lighting.PhySize;
            vm.PhyImpact = lighting.PhyImpact;
            vm.PhyDefend = lighting.PhyDefend;
            vm.AdpClass = lighting.AdpClass;
            vm.EleLgtPower = lighting.EleLgtPower;
            vm.AdpColor = lighting.AdpColor;
            vm.AdpCri = lighting.AdpCri;
            vm.OptAngle = lighting.OptAngle;
            vm.ProjSetup = lighting.ProjSetup;
            vm.BusMarket = lighting.BusMarket;

            if (!string.IsNullOrEmpty(lighting.PicFacade))
                vm.PicFacade = BusinessContext.Singleton.ImageUrl + lighting.PicFacade;

            if (!string.IsNullOrEmpty(lighting.PicSize))
                vm.PicSize = BusinessContext.Singleton.ImageUrl + lighting.PicSize;

            if (!string.IsNullOrEmpty(lighting.PicGrading))
                vm.PicGrading = BusinessContext.Singleton.ImageUrl + lighting.PicGrading;

            if (!string.IsNullOrEmpty(lighting.PicLive))
                vm.PicLive = BusinessContext.Singleton.ImageUrl + lighting.PicLive;
            return vm;
        }

        private async void Loading()
        {
            if (BusinessContext.Singleton.ParamIterator.Current is NaviTo4Param)
            {
                _parameter = (NaviTo4Param)BusinessContext.Singleton.ParamIterator.Current;

                this.DataContext = null;
                LoadRelations(_parameter.ID);
                WebServiceProxy proxy = new WebServiceProxy();
                PrimaryProd prod = await proxy.SearchPrimaryProdInfo(_parameter.ID);
                if (prod != null)
                    this.DataContext = ConvertForm(prod);
            }
        }

        private async void LoadRelations(string id)
        {
            Pictures.SelectionChanged -= Pictures_SelectionChanged;
            _items.Clear();

            WebServiceProxy proxy = new WebServiceProxy();
            PrimaryProdArray array = await proxy.SearchRelations(id);
            if (array != null)
            {
                foreach (PrimaryProd prod in array.PrimaryProdList)
                {
                    _items.Add(ConvertForm(prod));
                }
            }
            Pictures.SelectionChanged += Pictures_SelectionChanged;
        }

        public PageLevel PageLevel
        {
            get { return PageLevel.Fourth; }
        }

        public INavigate Navigation
        {
            get;
            set;
        }

        private void Viewbox_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            Viewbox vb = (Viewbox)sender;
            string value = vb.Tag == null ? null : vb.Tag.ToString();
            if (!string.IsNullOrEmpty(value))
            {
                PictureWindow pw = new PictureWindow(value);
                pw.Show();
            }
        }
    }
}
